package com.example.treehole.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.treehole.R;
import com.example.treehole.utils.DynamicColorUtils;
import com.example.treehole.viewobject.DynamicOutSide;
import com.example.treehole.widget.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;


public class DynamicHomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    //  private OnItemClickListener mListener;
    private List<DynamicOutSide> data;

    public void setData(List<DynamicOutSide> data) {
        this.data = data;
    }

    public DynamicHomeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public DynamicHomeAdapter(Context context, List<DynamicOutSide> data) {
        this.mContext = context;
        //  this.mListener = listener;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        // 1.首页无图  2.首页有图
        if (viewType == 1) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dynamic1, parent, false);
            return new LinearViewHolder1(view);
        } else if (viewType==2){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dynamic2, parent, false);
            return new LinearViewHolder2(view);
        }else
            return null;
    }

    @SuppressLint("WrongConstant")
    @Override
    // 通过getItemViewType的返回值来选择具体的item显示
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
            GradientDrawable drawable=new GradientDrawable();
            double score=data.get(position).getScore();
            //虽然本身cardView设置了圆角 但这里又设置背景颜色会将圆角顶掉
            drawable.setCornerRadius(50f);

            Map<String,Integer> colors= DynamicColorUtils.getItemColorsByScore(mContext,score);
            drawable.setColor(colors.get("bgColor"));
            int type=getItemViewType(position);
            if (type==1){
                LinearViewHolder1 vh= (LinearViewHolder1) holder;
                vh.itemView.setBackground(drawable);
                //设置颜色
                vh.tvTitle.setTextColor(colors.get("titleColor"));
                vh.tvTime.setTextColor(colors.get("timeColor"));
                vh.tvContent.setTextColor(colors.get("contentColor"));
                //填充内容
                vh.tvContent.setText(data.get(position).getTvContent());
                vh.tvTitle.setVisibility(View.VISIBLE);
                if (data.get(position).getTvTitle()!=null && !data.get(position).getTvTitle().equals(""))
                    vh.tvTitle.setText(data.get(position).getTvTitle());
                else
                    vh.tvTitle.setVisibility(View.GONE);
                vh.tvAuthor.setText(data.get(position).getTvAuthor());
                vh.tvTime.setText(data.get(position).getTvTime());
                Picasso.get().load(data.get(position).getIvAuthorHeader()).into(vh.ivAuthorHeader);
            }else if (type==2){
                LinearViewHolder2 vh= (LinearViewHolder2) holder;
                vh.itemView.setBackground(drawable);
                //设置颜色
                vh.tvTitle.setTextColor(colors.get("titleColor"));
                vh.tvTime.setTextColor(colors.get("timeColor"));
                vh.tvContent.setTextColor(colors.get("contentColor"));
                //填充内容
                vh.tvContent.setText(data.get(position).getTvContent());
                vh.tvTitle.setVisibility(View.VISIBLE);
                if (data.get(position).getTvTitle()!=null && !data.get(position).getTvTitle().equals(""))
                    vh.tvTitle.setText(data.get(position).getTvTitle());
                else
                    vh.tvTitle.setVisibility(View.GONE);
                vh.tvAuthor.setText(data.get(position).getTvAuthor());
                vh.tvTime.setText(data.get(position).getTvTime());
                Picasso.get().load(data.get(position).getIvAuthorHeader()).into(vh.ivAuthorHeader);
                Picasso.get().load(data.get(position).getIvContent()).into(vh.ivContent);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(position);
                }
            });

    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getType();
    }


    @Override
    public int getItemCount() {
        if (data==null) {
            return 0;
        }else{
            return data.size();
        }
    }


    static class LinearViewHolder1 extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvContent;
        CircleImageView ivAuthorHeader;
        TextView tvAuthor;
        TextView tvTime;

        public LinearViewHolder1(View view) {
            super(view);
            tvTitle=view.findViewById(R.id.tv_title);
            tvContent=view.findViewById(R.id.tv_content);
            ivAuthorHeader=view.findViewById(R.id.iv_author_header);
            tvAuthor=view.findViewById(R.id.tv_author);
            tvTime=view.findViewById(R.id.tv_time);
        }
    }

    static class LinearViewHolder2 extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvContent;
        ImageView ivContent;
        CircleImageView ivAuthorHeader;
        TextView tvAuthor;
        TextView tvTime;

        public LinearViewHolder2(View view) {
            super(view);
            tvTitle=view.findViewById(R.id.tv_title);
            tvContent=view.findViewById(R.id.tv_content);
            ivContent=view.findViewById(R.id.iv_content);
            ivAuthorHeader=view.findViewById(R.id.iv_author_header);
            tvAuthor=view.findViewById(R.id.tv_author);
            tvTime=view.findViewById(R.id.tv_time);
        }
    }

    //接口
    public interface OnItemClickListener {
        void onClick(int pos);
    }

    private OnItemClickListener listener;

    public void setOnMyItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


}


